using System;
using Volo.Abp.Domain.Entities.Auditing;

namespace CKY.AgentPlatform.Agents;

/// <summary>
/// 智能体类型枚举
/// </summary>
public enum AgentType
{
    /// <summary>
    /// 聊天助手
    /// </summary>
    Chat = 0,

    /// <summary>
    /// 智能助手
    /// </summary>
    Assistant = 1,

    /// <summary>
    /// 数据分析
    /// </summary>
    Analyst = 2,

    /// <summary>
    /// 创意生成
    /// </summary>
    Creative = 3,

    /// <summary>
    /// 自定义
    /// </summary>
    Custom = 4
}

/// <summary>
/// 智能体状态枚举
/// </summary>
public enum AgentStatus
{
    /// <summary>
    /// 已停止
    /// </summary>
    Stopped = 0,

    /// <summary>
    /// 运行中
    /// </summary>
    Running = 1,

    /// <summary>
    /// 启动中
    /// </summary>
    Starting = 2,

    /// <summary>
    /// 停止中
    /// </summary>
    Stopping = 3,

    /// <summary>
    /// 错误状态
    /// </summary>
    Error = 4
}

/// <summary>
/// 智能体实体 - AI Agent的核心领域模型
/// </summary>
public class Agent : AuditedAggregateRoot<Guid>
{
    /// <summary>
    /// 智能体名称
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// 智能体描述
    /// </summary>
    public string Description { get; set; }

    /// <summary>
    /// 智能体类型
    /// </summary>
    public AgentType AgentType { get; set; }

    /// <summary>
    /// 智能体状态
    /// </summary>
    public AgentStatus Status { get; set; }

    /// <summary>
    /// 系统提示词
    /// </summary>
    public string SystemPrompt { get; set; }

    /// <summary>
    /// 模型配置ID
    /// </summary>
    public Guid? ModelConfigId { get; set; }

    /// <summary>
    /// 提示词模板ID（可选）
    /// </summary>
    public Guid? PromptTemplateId { get; set; }

    /// <summary>
    /// 是否激活
    /// </summary>
    public bool IsActive { get; set; }

    /// <summary>
    /// 最后运行时间
    /// </summary>
    public DateTime? LastRunTime { get; set; }

    /// <summary>
    /// 运行次数统计
    /// </summary>
    public int RunCount { get; set; }

    /// <summary>
    /// 错误次数统计
    /// </summary>
    public int ErrorCount { get; set; }

    /// <summary>
    /// 配置信息（JSON格式）
    /// </summary>
    public string Configuration { get; set; }

    /// <summary>
    /// 标签（用于分类和搜索）
    /// </summary>
    public string Tags { get; set; }

    /// <summary>
    /// 使用次数统计
    /// </summary>
    public int UsageCount { get; set; }

    /// <summary>
    /// 最大令牌数
    /// </summary>
    public int MaxTokens { get; set; }

    /// <summary>
    /// 温度值（0-2）
    /// </summary>
    public double Temperature { get; set; }

    /// <summary>
    /// 超时时间（秒）
    /// </summary>
    public int TimeoutSeconds { get; set; }

    protected Agent()
    {
        // EF Core requires parameterless constructor
    }

    /// <summary>
    /// 创建新的智能体实例
    /// </summary>
    /// <param name="id">智能体ID</param>
    /// <param name="name">智能体名称</param>
    /// <param name="description">智能体描述</param>
    /// <param name="systemPrompt">系统提示词</param>
    /// <param name="modelConfigId">模型配置ID</param>
    public Agent(
        Guid id,
        string name,
        string description,
        string systemPrompt,
        Guid? modelConfigId) : base(id)
    {
        Name = name;
        Description = description;
        SystemPrompt = systemPrompt;
        ModelConfigId = modelConfigId;
        AgentType = AgentType.Chat;
        Status = AgentStatus.Stopped;
        IsActive = true;
        RunCount = 0;
        ErrorCount = 0;
        UsageCount = 0;
        MaxTokens = 4096;
        Temperature = 0.7;
        TimeoutSeconds = 30;
        Configuration = "{}";
        Tags = "";
    }

    /// <summary>
    /// 创建新的智能体实例（完整版）
    /// </summary>
    /// <param name="id">智能体ID</param>
    /// <param name="name">智能体名称</param>
    /// <param name="description">智能体描述</param>
    /// <param name="systemPrompt">系统提示词</param>
    /// <param name="modelConfigId">模型配置ID</param>
    /// <param name="promptTemplateId">提示词模板ID</param>
    public Agent(
        Guid id,
        string name,
        string description,
        string systemPrompt,
        Guid? modelConfigId,
        Guid? promptTemplateId) : base(id)
    {
        Name = name;
        Description = description;
        SystemPrompt = systemPrompt;
        ModelConfigId = modelConfigId;
        PromptTemplateId = promptTemplateId;
        AgentType = AgentType.Chat;
        Status = AgentStatus.Stopped;
        IsActive = true;
        RunCount = 0;
        ErrorCount = 0;
        UsageCount = 0;
        MaxTokens = 4096;
        Temperature = 0.7;
        TimeoutSeconds = 30;
        Configuration = "{}";
        Tags = "";
    }

    /// <summary>
    /// 启动智能体
    /// </summary>
    public void Start()
    {
        if (Status == AgentStatus.Running)
            return;

        Status = AgentStatus.Starting;
    }

    /// <summary>
    /// 停止智能体
    /// </summary>
    public void Stop()
    {
        if (Status == AgentStatus.Stopped || Status == AgentStatus.Stopping)
            return;

        Status = AgentStatus.Stopping;
    }

    /// <summary>
    /// 确认智能体已启动
    /// </summary>
    public void ConfirmStarted()
    {
        Status = AgentStatus.Running;
        LastRunTime = DateTime.UtcNow;
        RunCount++;
    }

    /// <summary>
    /// 确认智能体已停止
    /// </summary>
    public void ConfirmStopped()
    {
        Status = AgentStatus.Stopped;
    }

    /// <summary>
    /// 设置错误状态
    /// </summary>
    /// <param name="errorMessage">错误信息</param>
    public void SetError(string errorMessage)
    {
        Status = AgentStatus.Error;
        ErrorCount++;
        // 可以将错误信息记录到Configuration中或专门的错误日志中
    }

    /// <summary>
    /// 更新配置信息
    /// </summary>
    /// <param name="configuration">配置JSON字符串</param>
    public void UpdateConfiguration(string configuration)
    {
        Configuration = configuration ?? "{}";
    }

    /// <summary>
    /// 更新标签
    /// </summary>
    /// <param name="tags">标签字符串，逗号分隔</param>
    public void UpdateTags(string tags)
    {
        Tags = tags ?? "";
    }

    /// <summary>
    /// 检查智能体是否可以运行
    /// </summary>
    /// <returns>是否可以运行</returns>
    public bool CanRun()
    {
        return IsActive && Status != AgentStatus.Running && Status != AgentStatus.Starting;
    }

    /// <summary>
    /// 获取智能体统计信息
    /// </summary>
    /// <returns>统计信息字符串</returns>
    public string GetStatistics()
    {
        return $"运行次数: {RunCount}, 错误次数: {ErrorCount}, 成功率: {(RunCount > 0 ? (RunCount - ErrorCount) * 100.0 / RunCount : 100):F1}%";
    }

    /// <summary>
    /// 设置状态
    /// </summary>
    /// <param name="status">新状态</param>
    public void SetStatus(AgentStatus status)
    {
        Status = status;
    }

    /// <summary>
    /// 激活智能体
    /// </summary>
    public void Activate()
    {
        IsActive = true;
    }

    /// <summary>
    /// 停用智能体
    /// </summary>
    public void Deactivate()
    {
        IsActive = false;
    }

    /// <summary>
    /// 重置统计信息
    /// </summary>
    public void ResetStats()
    {
        RunCount = 0;
        ErrorCount = 0;
        UsageCount = 0;
        LastRunTime = null;
    }
}